import axios from "axios"
import { ElMessage  } from 'element-plus'
export  const request =(options:any)=> {
    return new Promise((resolve, reject) => {
        // create an axios instance
        const service = axios.create({
            baseURL:'/api',
            timeout: 80000 
        })
        // request interceptor
        service.interceptors.request.use(
            (config:any) => {
                let token:string | null =sessionStorage.getItem("token")
                if (token) {
                    config.headers['token'] = token
                    config.headers.Authorization =  + token
                }
                return config
            },
            (error:any) => {
                console.log(error)
            }
        )
        service.interceptors.response.use(
            (response:any) => {
                return response.data
            }, (error:any) => {
                if(error.request.responseType == 'blob'){
                    ElMessage.error('导出失败')
                    return Promise.reject(error)
                }else{
                    if(error.response.status == 500){
                        console.log(error)
                        ElMessage.error(error.response.data.errMsg)
                    }else{
                        ElMessage.error('服务器请求错误，请稍后再试')
                    }
                    return Promise.reject(error)
                }
            }
        )
        // 请求处理
        service(options)
            .then((res:any) => {
                resolve(res)
            })
            .catch((error:any) => {
                reject(error)
            })
    })
}
export default request
